package com.dantfb.services;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dantfb.DAO.PostsDAO;
import com.dantfb.post.Post;

public class PostsServlet extends HttpServlet {
	HttpSession session;

	public void init() throws ServletException {
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (request.getParameter("postContent") != null) {
			String postContent = request.getParameter("postContent");
			String postID = UUID.randomUUID().toString();
			Date date = new Date();
			String author =(String) request.getSession().getAttribute("UUID");
			if (!postContent.trim().equals("")) {
				PostsDAO.getInstance().addPost(
					new Post(postID, date.toLocaleString(), author,
									postContent, null, null, 0,
									null));
					response.sendRedirect("home.jsp?NOTIF=POST_HAS_BEEN_CREATED");
			} else {
				response.sendRedirect("home.jsp?NOTIF=POST_MUST_NOT_BE_EMPTY");
			}
		} else if (request.getParameter("nbLike") != null) {
			String postId = request.getParameter("postId");
			String nbLike = request.getParameter("nbLike");
			System.out.println(postId + " " + nbLike);
			PostsDAO.getInstance().incrementLike(postId, nbLike);
			response.sendRedirect("home.jsp");
		}
	}

	public void destroy() {
	}

}
